<?php if ( ! defined('APPPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/

class misc_controller extends PZ_Controller{

	public function __construct() {
		parent::__construct();
		$this->authfunctions = array('apply','ajaxmessage');
	}

	public function email(){
		load_api('email')->send();
	}

	public function switchcolor(){
		if(($theme=$this->param('theme'))&&($color = $this->param('color'))){
			$existedstyles = $this->settings['existedstyles'];
			if($existedstyles[$theme][$color]){
				$this->input->set_cookie('pz_style', $theme.':'.$color, 86400*365);
			}
		}
		$url = (!$_SERVER['HTTP_REFERER'])?site_url(config_item('default_module')):$_SERVER['HTTP_REFERER'];
		$this->jump($url);
	}


	public function switchlayout(){
		//$type : wf = waterfall ls=list
		if(($mdl=$this->param('mdl'))&&($type = $this->param('type'))){
			$this->input->set_cookie('pz_layout_'.$mdl, $type, 86400*365);
		}
		$url = (!$_SERVER['HTTP_REFERER'])?(($mdl)?site_url($mdl.'/index'):site_url(config_item('default_module'))):$_SERVER['HTTP_REFERER'];
		$this->jump($url);
	}

	public function apply(){
		if(($message = $this->param('apply_reason'))&&($category_id=$this->param('apply_cid'))){
			$applytype = $this->param('applytype');
			$db = load_db('misc/apply');
			if($applytype=='star'){
				if($db->checkstar_exits($this->cuser['uid'])){
					$this->ajax_failed_response(lang('already_staruser_failed','tip'));
					return;
				}else {
					$db->add_starapply($this->cuser['uid'],$category_id,$message);
					$this->ajax_success_response(array('url'=>site_url('star/show')),lang('200_submitsuccess','tip'));
					return;
				}
					
			}elseif($applytype=='store'){
				load_db('misc/apply')->add_storeapply($this->cuser['uid'],$category_id,$message);
			}elseif($applytype=='groupadmin'){
				if($db->groupadmin_exits($this->cuser['uid'],$category_id)){
					$this->ajax_failed_response(lang('already_applied_failed','tip'));
					return;
				}else {
					load_db('misc/apply')->add_groupadminapply($this->cuser['uid'],$category_id,$message);
					$this->ajax_success_response(null,lang('200_submitsuccess','tip'));
					return;
				}
			}
		}
		$this->ajax_failed_response(lang('500_operatefaild','tip'));
		return;
	}

	public function ajaxtpl(){
		$tpl = $this->param('tpl');
		$tpls = array('address_input','apply_common','apply','avatar','banner','comment_detail','comment_store','comment_waterfall',
				'crop_dialog','edit_album','edit_item','footer_count','forwarding','login_box','message','confirm','deepcategory',
				'publish_select','pubish','push_dialog','register_box','star_open_confirm','star_open','static_include','maketop','digist',
				'swfupload_thumb_error','swfupload_thumb_fetch','swfupload_thumb_others','swfupload_thumb','theme_select','video_thumb','tags_pop');
		if($tpl&&in_array($tpl, $tpls)){
			if(in_array($tpl, array('apply','star_open_confirm'))){
				$this->starcateogries = load_db("star/starcategory")->getAllCategories();
			}else if($tpl=='theme_select'){
				$data['userthemes'] = load_api('setting')->getSetting('usertheme');
				$data['css'] = ucache('r', 'css', $this->cuser['uid']);
			}else if($tpl=='login_box'||$tpl=='register_box'){
				$this->vcodesetting = load_api('setting')->getVcodeSetting();
				$this->socialapis = load_api('setting')->getSetting('socialapi');
			}else if($tpl=='footer_count'){
				$share = load_api('share');
				$comment = load_api('comment');
				load_helper('view');
				$vdata = $share->getViewCount();
				if(in_array('click', $this->settings['counter'])){
					$data['click'] = zeronum($vdata['click']);
				}
				if(in_array('like', $this->settings['counter'])){
					$data['like'] = zeronum($vdata['like']);
				}
				if(in_array('share', $this->settings['counter'])){
					$data['share'] = zeronum($vdata['share']);
				}
				if(in_array('comment', $this->settings['counter'])){
					$data['comment'] = zeronum(load_api('comment')->getTotalCount());
				}
			}else if($tpl=='push_dialog'){
				$this->categories = load_api('category')->getCategoryWithHomeShares();
			}else if($tpl=='deepcategory'){
				$this->categorytree = load_api('category')->getCategoryDeepTreeTpl();
			}
			$res['tpl'] = $this->render('js_tpl/'.$tpl.'_tpl',$data);
			$this->ajax_success_response($res, '');
			return;
		}
		$this->ajax_failed_response(lang('404_tpl','tip'));
	}


	public function vcode(){
		load_class('Captcha')->CreateImage();
	}

	public function vcheck(){
		$this->vcodesetting = load_api('setting')->getVcodeSetting();
		if(parent::vcheck()) {
			$this->ajax_echo('true');
		}else{
			$this->ajax_echo('false');
		}
	}

	public function smiles(){
		$api = load_api("setting");
		$smiles = $api->getSmilesWithCode();
		if($smiles){
			$this->ajax_success_response($smiles['smiles']);
		}else{
			$this->ajax_failed_response();
		}
	}

	public function ajaxmessage(){
		$event = $this->param('event');
		$event_arr = array('reward','alert','warn','user_message');
		if(!in_array($event, $event_arr)) return;
		eval("\$this->$event();");
	}
	private function reward(){
		$eventdb = load_db('event');
		$start_time = TIMESTAMP-5*24*3600;
		$eventdb->clean_log($this->cuser['uid'],$start_time);
		$log = $eventdb->getLastestEventByUser($this->cuser['uid']);
		if($log){
			$messages = array();
			$msg = lang($log['event_code'],'event').' ';
			$setting = load_api('setting')->getSetting('credit');
			for ($i=1;$i<=3;$i++){
				$key = 'ext_credits_'.$i;
				if($log[$key]>0){
					$msg .= $setting[$key].' +'.$log[$key].' ';
				}elseif ($msg["ext_credits_$i"]<0){
					$msg .= $setting[$key].' -'.$log[$key].' ';
				}
			}
			$messages[] = $msg;
			$conditions_update['user_id']=$this->cuser['uid'];
			$conditions_update['is_read']='0';
			$eventdb->update($conditions_update,array('is_read'=>1));
			$this->ajax_success_response($messages, 'tip');
			return;
		}
		$this->ajax_success_response(null);
	}

	private function user_message(){
		$ptx_message = load_db('message');
		$message_num=10;
		$message_limit = $ptx_message->clean_message_num($this->cuser['uid'],$this->memperm['maxmessage']);
		$conditions['to_user_id']=$this->cuser['uid'];
		$conditions['is_read']='0';
		$message_count = $ptx_message->findCount($conditions);

		if($message_count > 99){
			$message_html = "<em>N</em>";
		}elseif ($message_count){
			$message_html = "<em>$message_count</em>";
		}else{
			$message_html = "";
		}
		$this->ajax_success_response(array('message_html'=>$message_html), 'user_message');
	}

	private function alert(){
		$message_count = load_db('event/activity')->getActivityCountByToUser($this->cuser['uid']);
		if($message_count > 99){
			$message_html = "<em>N</em>";
		}elseif ($message_count){
			$message_html = "<em>$message_count</em>";
		}else{
			$message_html = "";
		}
		$this->ajax_success_response(array('message_html'=>$message_html), 'alert');
	}

	public function randad(){
		$ad_position = $this->param("ad_position");
		$setting = load_api('setting')->getSetting('ads');
		$ads_array = $setting[$ad_position];
		$this->ad = rand_pop($ads_array);
		$this->output('misc/adproxy');
	}

	public function adproxy(){
		$key = $this->param("key");
		$ad_position = $this->param("ad_position");
		$setting = load_api('setting')->getSetting('ads');
		$ads_array=$setting[$ad_position];
		foreach ($ads_array as $ads){
			if($ads['key'] == $key){
				$ads_show = $ads;
				break;
			}
		}
		$this->ad = $ads_show;
		$this->output('misc/adproxy');
	}

}



